package com.mq.mqtest;

import com.mq.mqtest.threadpool.ThreadPool;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.*;
import java.util.logging.Handler;

@SpringBootApplication
public class MqtestApplication {

    public static void main(String[] args) {
        SpringApplication.run(MqtestApplication.class, args);
        initThreadPool();
    }

    private static void initThreadPool() {
        int coreThreadSize = 1;
        int maxSize = 1;
        long keepAliveTime = 1;

        //可以自定义各种拒绝策略 TODO
        RejectedExecutionHandler rejectPolicy = (runnable, executor) -> {

            //1、默认的抛异常策略
            //throw new RuntimeException("抛异常..." + runnable);
            //2、死等
            // executor.getQueue().put(runnable);
            //3、我自己执行
            // runnable.run();
            //4、....带超时时间的等。。。
        };

        //线程工厂
        ThreadFactory threadFactory = (runnable) -> new Thread(runnable, "t1");

        //init thread pool
        new ThreadPoolExecutor(
            coreThreadSize, maxSize,
            keepAliveTime, TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(),
            threadFactory, rejectPolicy);
    }

}
